package control;

import System.MainControl;
import System.SysMode;
import System.SystemObject;

import BaseClasses.User;


/**control of login*/
public class loginControlNew extends abstractControl  
{
	private SystemObject container;
	private String Msg;
	private int resultMode = -1;
	
	public loginControlNew(){}
	
	public boolean createLogin(int Port, String Host)
	{
	
		try {
			MainControl.setClientConnection(Host,Port);
		} catch (Exception e) 
		{
			return false;
		}
		return true;
	
		
	}

	@Override
	public boolean sendToServer(Object obj) 
	{
		Object[] result;
		
		container = new SystemObject(obj, SysMode.User_Login);
		MainControl.client.send(container);
		container = (SystemObject) MainControl.client.getResult();
		if (container.getMode() == SysMode.OK)
		{
			MainControl.setCurrentUser((User) ((Object[]) container.getObj())[0]);
			return true;
		}
		else
		{
			result = (Object[]) container.getObj();
			if (result == null)
			{
				Msg = "Login failed, check the ID or the password and try again";
				resultMode = 0;
			}
			else
			{
				if (result[0] instanceof String)
				{
					Msg = (String) result[0];
					resultMode = 1;
				}
				if (result[1] instanceof String)
				{
					Msg = (String) result[1];
					resultMode = 2;
				}
			}
			return false;
		}
	}
	
	public int getMode() {return resultMode;}
	public String getMsg() {return Msg;}
	





	
	

}
